package com.wolf.create.method;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Map;

import com.wolf.create.model.DataModel;

import org.apache.commons.lang3.*;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

public class CreateMethod_lw implements CreateMethod {

	private DataModel pojoModel;

	public CreateMethod_lw(DataModel pojoModel) {
		this.pojoModel = pojoModel;
	}

	public CreateMethod_lw() {
	}

	public ArrayList<?> execMethod(ArrayList<String> createSelect) {
		ArrayList<?> msg = new ArrayList<Map<String, Boolean>>();
		// 创建Freemarker配置实例
		Configuration cfg = new Configuration(new Version("2.3.23"));
		try {
			// 项目路径
			String proBin = CreateMethod_lw.class.getClass().getResource("/").getFile();
			// String packageName = new CreateMethod().getClass().getPackage().getName();
			String packageName = "";
			cfg.setDirectoryForTemplateLoading(
					new File(proBin + "/" + packageName.replaceAll("\\.", "/") + "/templates/" + file_suffix()));
		} catch (IOException e) {
			e.printStackTrace();
		}

		// System.out.println(pojoModel.getFullPackagePath());

		// pojoModel.setPojoPackagePath(pojoFile.toString());
		// 实体类包名
		// pojoModel.setPojoPackageName(pojoModel.getPackageName() + "." +
		// getCamelStr(dbcp.getTableName()) + ".model");

		if (createSelect.contains("Model")) {
			createModel(pojoModel, cfg);
		}
		if (createSelect.contains("Mapper")) {
			createDao(pojoModel, cfg);
		}
		if (createSelect.contains("Mapping")) {
			createMapping(pojoModel, cfg);
		}
		if (createSelect.contains("Service")) {
			createService(pojoModel, cfg);
		}
		if (createSelect.contains("ServiceImpl")) {
			createServiceImpl(pojoModel, cfg);
		}
		if (createSelect.contains("Controller")) {
			createController(pojoModel, cfg);
		}
		// if (createSelect.contains("ListPage")) {
		// createListPage(pojoModel, cfg);
		// }

		// for (String s : createSelect) {
		// System.out.println(s);
		// }

		return msg;
	}

	// 生成ListPage查看页面
	public static boolean createListPage(DataModel pojoModel, Configuration cfg) {
		// 加载模板文件
		Template t = null;
		BufferedWriter rd = null;
		OutputStream out = null;
		try {
			t = cfg.getTemplate("listPage.ftl");
			// 实体类路径
			File pojoFile = new File(pojoModel.getPagePath());
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "ListPage.jsp");
			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			System.out.println("ListPage文件所在路径：" + file.getAbsolutePath());
			rd.close();
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {

			e.printStackTrace();
			return false;
		}
	}

	public String file_suffix() {
		return this.getClass().getSimpleName().split("_")[1];
	}

	public static void main(String[] args) throws IOException {
		CreateMethod_lw c = new CreateMethod_lw();
		System.out.println(c.file_suffix());
		// 获取项目路径
		String projectPath = System.getProperty("user.dir");
		System.out.println(projectPath);
		// String path =
		// this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		// System.out.println(path);
		String a = ClassLoader.getSystemResource("").getPath();
		System.out.println("------>" + a);
		File directory = new File("");// 设定为当前文件夹
		String path1 = System.getProperty("java.class.path");
		System.out.println(path1);
		String packageName = CreateMethod_lw.class.getPackage().getName();
		System.out.println(packageName);
		System.out.println(directory.getCanonicalPath());// 获取标准的路径
		System.out.println(directory.getAbsolutePath());// 获取绝对路径
	}

	@Override
	public String createModel(DataModel pojoModel, Configuration configuration) {
		// 加载模板文件
		Template t = null;
		BufferedWriter rd = null;
		OutputStream out = null;
		try {
			t = configuration.getTemplate("model.ftl");
			// 实体类路径
			File pojoFile = new File(pojoModel.getFullPackagePath() + "/model");
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + ".java");
			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			rd.close();
			out.flush();
			out.close();

			t = configuration.getTemplate("modelCondition.ftl");
			File fileCon = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "Condition.java");
			out = new FileOutputStream(fileCon);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			rd.close();
			out.flush();
			out.close();
			return "model文件所在路径：" + file.getAbsolutePath();
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}

	@Override
	public String createDao(DataModel pojoModel, Configuration configuration) {
		// 加载模板文件
		Template t = null;
		BufferedWriter rd = null;
		OutputStream out = null;
		try {
			t = configuration.getTemplate("mapper.ftl");
			// 实体类路径
			File pojoFile = new File(pojoModel.getFullPackagePath() + "/mapper");
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "Mapper.java");
			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);

			rd.close();
			out.flush();
			out.close();

			System.out.println("mapper文件所在路径：" + file.getAbsolutePath());
			return "mapper文件所在路径：" + file.getAbsolutePath();
		} catch (Exception e) {
			// e.printStackTrace();
			return null;
		}
	}

	@Override
	public String createMapping(DataModel pojoModel, Configuration configuration) {
		// 加载模板文件
		Template t = null;
		BufferedWriter rd = null;
		OutputStream out = null;
		try {
			t = configuration.getTemplate("mapping.ftl");
			// 实体类路径
			File pojoFile = new File(pojoModel.getFullPackagePath() + "/mapping");
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "Mapping.xml");
			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			rd.close();
			out.flush();
			out.close();

			System.out.println("mapping文件所在路径：" + file.getAbsolutePath());
			return "mapping文件所在路径：" + file.getAbsolutePath();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String createService(DataModel pojoModel, Configuration configuration) {
		// 加载模板文件
		Template t = null;
		BufferedWriter rd = null;
		OutputStream out = null;
		try {
			t = configuration.getTemplate("service.ftl");
			// 实体类路径
			File pojoFile = new File(pojoModel.getFullPackagePath() + "/service");
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "Service.java");
			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			rd.close();
			out.flush();
			out.close();
			return "service文件所在路径：" + file.getAbsolutePath();
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String createServiceImpl(DataModel pojoModel, Configuration configuration) {
		// 加载模板文件
		Template t = null;
		BufferedWriter rd = null;
		OutputStream out = null;
		try {
			t = configuration.getTemplate("serviceImpl.ftl");
			// 实体类路径
			File pojoFile = new File(pojoModel.getFullPackagePath() + "/serviceImpl");
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "ServiceImpl.java");
			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			rd.close();
			out.flush();
			out.close();

			System.out.println("serviceImpl文件所在路径：" + file.getAbsolutePath());
			return "serviceImpl文件所在路径：" + file.getAbsolutePath();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String mkdirFile(String ftlName, String methodType, Configuration configuration) {
		// 加载模板文件
		Template t;
		BufferedWriter rd;
		OutputStream out;
		String filePath = null;
		try {
			t = configuration.getTemplate(ftlName);
			// 实体类路径
			File pojoFile = new File(pojoModel.getFullPackagePath() + "/" + methodType);
			if (!pojoFile.exists()) {
				pojoFile.mkdirs();
			}
			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() +StringUtils.substring(methodType,0,1).toUpperCase() +StringUtils.substring(methodType, 1) + ".java");

			out = new FileOutputStream(file);
			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
			t.process(pojoModel, rd);
			filePath=file.getAbsolutePath();
			rd.close();
			out.flush();
			out.close();

		} catch (Exception e) {
			// e.printStackTrace();
		}
		return filePath;
	}

	@Override
	public String createController(DataModel pojoModel, Configuration configuration) {
		
		return "controller文件所在路径：" + mkdirFile("controller.ftl","controller",configuration);
		
		// 加载模板文件
//		Template t;
//		BufferedWriter rd;
//		OutputStream out;
//		try {
//			t = configuration.getTemplate("controller.ftl");
//			// 实体类路径
//			File pojoFile = new File(pojoModel.getFullPackagePath() + "/controller");
//			if (!pojoFile.exists()) {
//				pojoFile.mkdirs();
//			}
//			System.out.println(pojoFile.getAbsolutePath());
//			File file = new File(pojoFile.getAbsolutePath() + "/" + pojoModel.getEntityName() + "Controller.java");
//
//			out = new FileOutputStream(file);
//			rd = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
//			t.process(pojoModel, rd);
//
//			rd.close();
//			out.flush();
//			out.close();
//
//			System.out.println("controller文件所在路径：" + file.getAbsolutePath());
//			return "controller文件所在路径：" + file.getAbsolutePath();
//		} catch (Exception e) {
//			// e.printStackTrace();
//			return null;
//		}
	}

}